package com.ewanghuiju.app.presenter.home;

import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.home.ChoicesContract;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.local.HomeLimitedSeckillLocalTop;
import com.ewanghuiju.app.model.bean.response.ArticleArticleResponBean;
import com.ewanghuiju.app.model.bean.response.MailShopResponBean;
import com.ewanghuiju.app.model.bean.response.PintuanClassifyResponBean;
import com.ewanghuiju.app.model.bean.response.PintuanShopListResponBean;
import com.ewanghuiju.app.model.http.exception.ApiException;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import java.util.List;

import javax.inject.Inject;

/**
 * @author sj  qq:751943509
 * @class 精选
 * @time 2020/2/16
 */
public class ChoicesPresenter extends RxPresenter<ChoicesContract.View> implements ChoicesContract.Presenter {

    private DataManager mDataManager;

    private int currentPage = 1;

    @Inject
    public ChoicesPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    /**
     * 获取商品图
     * 1 首页轮播
     */
    @Override
    public void getChoicesData(int type) {
        addSubscribe(mDataManager.taokeGetlbimg(type + "")
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        mView.showLbImg(type, optional.getIncludeNull().getBanner());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );

//        addSubscribe(
//                mDataManager.taokeGetlbimg()
//                        .mergeWith(mDataManager.articleArticlelist())
////                .mergeWith(mDataManager.articleArticlelist())
//                .compose(RxUtil.rxSchedulerHelper())
//                .compose(RxUtil.handleMyResultCannull())
//                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
//                    @Override
//                    public void onNext(Optional<BaseResponse> optional) {
//                        LogUtil.i(new Gson().toJson(optional));
//                        Object obj = optional.getIncludeNull();
//                        if (obj instanceof  BaseResponse) {
//                            BaseResponse br = (BaseResponse)obj;
//                            if(br.getBanner()!=null&&br.getBanner().size()>0){
//
//                            }
////                            mView.showLbImg(optional.getIncludeNull().getBanner());
//                        }
//                        LogUtil.i("合并请求返回的次数");
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        super.onError(e);
//                    }
//                })
//        );

    }

    /**
     * 获取轮播头条
     */
    @Override
    public void getViewFlipper() {
        addSubscribe(mDataManager.articleArticlelist()
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<ArticleArticleResponBean>>>(mView) {
                    @Override
                    public void onNext(Optional<List<ArticleArticleResponBean>> optional) {
                        mView.showViewFlipper(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                        } else {
                        }
                    }
                })
        );
    }

    /**
     * 获取限时抢购
     */
    @Override
    public void getLimitedSeckill() {
        addSubscribe(mDataManager.getTbqg()
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<HomeLimitedSeckillLocalTop>>(mView) {
                    @Override
                    public void onNext(Optional<HomeLimitedSeckillLocalTop> optional) {
                        mView.showLimitedSeckillList(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                        } else {
                        }
                    }
                })
        );
    }

    /**
     * 获取猜你喜欢
     */
    @Override
    public void getTbyoulike() {
        currentPage = 1;
        addSubscribe(mDataManager.getTbyoulike(currentPage)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<MailShopResponBean>>>(mView) {
                    @Override
                    public void onNext(Optional<List<MailShopResponBean>> optional) {
                        mView.showTbyoulike(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                        } else {
                        }
                    }
                })
        );
    }

    @Override
    public void getMoreHomeData() {

    }

    @Override
    public void bindJpushAlias() {
        //绑定极光id 弃用

    }

    /**
     * 获取热门推荐 --拼团
     *
     * @param page
     */
    @Override
    public void getPintuanList(int page) {
        addSubscribe(mDataManager.getPtiGoodsList(page)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<PintuanShopListResponBean>>>(mView) {
                    @Override
                    public void onNext(Optional<List<PintuanShopListResponBean>> optional) {
                        if (mView != null)
                            mView.showPintuanList(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null)
                            mView.showPintuanListFail();
                    }
                })
        );
    }

    @Override
    public void getPticatesAll() {

        addSubscribe(mDataManager.getPticatesAll()
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<PintuanClassifyResponBean>>>(mView) {
                    @Override
                    public void onNext(Optional<List<PintuanClassifyResponBean>> optional) {
                        mView.showPticatesAll(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );

    }

    @Override
    public void ptiLucydrawCheck() {
        //检测红包
        addSubscribe(mDataManager.ptiLucydrawCheck()
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView,true,false) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if(mView!=null)
                            mView.showPtiLucydrawCheck(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if(mView!=null)
                            if (e instanceof ApiException) {
                                mView.showPtiLucydrawCheckError(((ApiException) e).getCode(),e.getMessage());
                            }

                    }
                })
        );
    }

    @Override
    public void ptiLuckydraw() {
        //打开红包
        addSubscribe(mDataManager.ptiLuckydraw()
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if(mView!=null)
                            mView.showPtiLuckydraw(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void ptiLuckyDrawoff() {
        //红包开关
        addSubscribe(mDataManager.ptiLuckyDrawoff()
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView,true,false) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if(mView!=null)
                            mView.showPtiLuckyDrawoff(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void attachView(ChoicesContract.View view) {
        super.attachView(view);
    }
}
